#!/bin/bash
funcs=`which functions.sh`
if [ "$funcs" != "" ] ;then
    . $funcs
else
    funcs=`dirname $0`/functions.sh
    echo "$PWD"
    echo "funcs=$funcs"
    if [ "$funcs" != "" ] ;then
	. $funcs
    else
	exit
    fi
fi
SIM2IMG=out/host/linux-x86/bin/simg2img
mount_point=~/mnt/system/
list_file1=etc/all-appfiles
system_img=$1
out_dir=$2
if [ -z "$system_img" ]; then
  echoTextRed "$0 /path/to/system.img $PWD/release_system"
  exit
fi
if [ -z "$out_dir" ];then
  out_dir=`question  "please give $system_img ?......"  2  $PWD/release_system`
fi
rm  $out_dir -rf
mkdir -pv $out_dir
result=`file $system_img |grep ext4 |grep filesystem`
if [ -z "$result" ]; then
  if test -f $SIM2IMG; then
    $SIM2IMG $system_img ${system_img}_release
  else
    echoTextRed "不存在 $SIM2IMG"
    exit
  fi
  mkdir -pv $mount_point
  sudo.shc mount -o loop ${system_img}_release $mount_point
else
  mkdir -pv $mount_point
  sudo.shc mount -o loop $system_img $mount_point
fi

if test -f $mount_point/$list_file1; then
  cd $mount_point
  for f in `cat $mount_point/$list_file1`
  do
    cp --parents -vrufL $f $out_dir
  done
  cd -
else
   echoTextRed "镜像文件 $system_img 中不存在 $mount_point/$list_file1 文件"
  exit
fi
sudo.shc umount $mount_point
echoTextBlue  "提取出的文件存放在目录: $out_dir"
echoTextBlue  "用以下命令重新合并,生成system镜像"
echoTextGreen "mkimg-merge-release-apps.sh $out_dir/  /path/to/your/android/src/out/xxx/system/"

